﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#ifndef QT_BALLOONTIP_H
#define QT_BALLOONTIP_H

#include "GmUiCommonComponent.h"
#include "OwnerPtr.h"

#include <QWidget>

namespace gcmp
{

class GM_UI_COMMON_COMPONENT_EXPORT QtBalloonTip : public QWidget
{
    Q_OBJECT

public:
    struct Parameter    // 控制气泡提示行为的参数
    {
        Parameter();

        bool useIcon;
        QPixmap icon;
        bool useTitle;
        std::wstring title;
        std::wstring message;
        int timeout;
        bool showArrow;                     // 气泡有没有尖角
    };

public:
    QtBalloonTip(QWidget* pOwnerWidget, const Parameter& parameter);
    ~QtBalloonTip();

    QWidget* GetOwnerWidget() const;
    void ShowBalloon();

    void SetBalloonTip(const Parameter& parameter);

    bool eventFilter(QObject* pWatched, QEvent* pEvent) override;

protected:
    void paintEvent(QPaintEvent* pEvent) override;
    void timerEvent(QTimerEvent* pEvent) override;

private:
    QPainterPath GetBalloonPath(const QPoint& pos);

private:
    QWidget* m_pOwnerWidget;
    Parameter m_parameter;
    int m_timerId;
    QPixmap m_borderPixmap;
    bool m_ownerWindowAboutToDeactivate;
    gcmp::OwnerPtr<QSignalBlocker> m_pOwnerWidgetSignalBlocker;
};

}   // end namespace gcmp

#endif // QT_BALLOONTIP_H
